// 用户
// noinspection JSUnusedLocalSymbols

import {MyCache} from "@ginstone/common-utils/dist/src/utils/MyCache";
import {NukeApi} from "@ginstone/nga-api/dist/src/method/NukeApi";
import {SignatureSetParam} from "@ginstone/nga-api/dist/src/params/NukeParams";
import {ElementUtils} from "@/assets/js/ElementUtils";

export default {
    namespaced: true,
    state: {
        // 缓存
        cache: new MyCache({limit: 60 * 30}),
        // 缓存用户的账号状态
        accountStatus: {},
        // 缓存用户IP属地
        ipLocation: {},
    },
    mutations: {
        method(state, payload) {
        },
        // 更新用户的账号状态(从 UserContext.userInfo 中获得
        updateAccountStatus(state, userInfo) {
            userInfo.forEach(({userId, accountStatus}) => {
                state.accountStatus[userId] = accountStatus
            })
        },
        // 处理获取到的用户数据
        handleUserInfo(state, info) {
            //缓存IP属地
            state.ipLocation[info.userId] = info.ipLocation;
            state.ipLocation[info.username] = info.ipLocation;
        },

    },
    actions: {
        method: ({dispatch, commit, state, rootGetters}, payload) => {
            const client = rootGetters["Account/getClient"]();
        },
        // 设置签名
        signatureSet: ({dispatch, commit, state, rootGetters}, {userId, sign}) => {
            const client = rootGetters["Account/getClient"]();
            return NukeApi.signatureSet(client, new SignatureSetParam(userId, sign)).then(message => {
                ElementUtils.notice({message})
                return message;
            }).catch(ElementUtils.exceptionHandler)
        },
        medalInfoList: ({dispatch, commit, state, rootGetters}, {medalId, force}) => {
            const client = rootGetters["Account/getClient"]();
            const method = () => NukeApi.medalInfoList(client, medalId);
            return state.cache.use({medalId}, method, force)
        },
        // 根据用户id获取用户信息
        userInfoGetById: ({dispatch, commit, state, rootGetters}, {id, force}) => {
            const client = rootGetters["Account/getClient"]();
            const method = () => NukeApi.userInfoGetById(client, id);
            return state.cache.use({id}, method, force)
                .then(res => {
                    commit("handleUserInfo", res);
                    return res;
                })
        },
        // 根据用户名获取用户信息
        userInfoGetByName: ({dispatch, commit, state, rootGetters}, {username, force}) => {
            const client = rootGetters["Account/getClient"]();
            const method = () => NukeApi.userInfoGetByName(client, username);
            return state.cache.use({username}, method, force).then(res => {
                commit("handleUserInfo", res);
                return res;
            })
        },

    },
    getters: {},
}